package com.sgb.kjwl.view.ui.activity.house;

import android.content.Intent;
import android.text.TextUtils;

import androidx.databinding.DataBindingUtil;
import androidx.recyclerview.widget.LinearLayoutManager;

import com.sgb.kjwl.AppActivity;
import com.sgb.kjwl.R;
import com.sgb.kjwl.databinding.ActivityLeasePriceSetBinding;
import com.swgk.core.util.PickSelectEntity;
import com.sgb.kjwl.model.entity.lease.PriceSetEntity;
import com.sgb.kjwl.view.ui.adapter.lease.PriceSetAdapter;
import com.swgk.core.util.MToast;

import java.io.Serializable;
import java.util.ArrayList;
import java.util.List;

/**
 * 租赁-价格设置
 */
public class LeasePriceSetActivity extends AppActivity {
    private ActivityLeasePriceSetBinding binding;

    private List<PickSelectEntity<List<PickSelectEntity>>> timeUnitList = new ArrayList();
    private List<PriceSetEntity> priceSetList = new ArrayList<>();

    private PriceSetAdapter priceSetAdapter;
    private boolean isDriver = false;
    private double maxPrice;
    private String categoryType = "0";
    private String driverText = "驾驶员";

    @Override
    protected void initView() {
        binding = DataBindingUtil.setContentView(this, R.layout.activity_lease_price_set);
        binding.topLayout.titleView.setText("价格设置");
        binding.topLayout.ivBack.setOnClickListener(v -> finish());
        binding.btnSave.setOnClickListener(v -> validateData());//立即发布按钮
    }

    @Override
    protected void init() {
        timeUnitList = (List<PickSelectEntity<List<PickSelectEntity>>>) getIntent().getSerializableExtra("timeUnitList");
        priceSetList = (List<PriceSetEntity>) getIntent().getSerializableExtra("priceSetList");
        isDriver = getIntent().getBooleanExtra("isDriver", false);
        maxPrice = getIntent().getDoubleExtra("maxPrice", 0);
        categoryType = getIntent().getStringExtra("categoryType");
        if ("0".equals(categoryType)) {
            driverText = "驾驶员";
        }else if ("1".equals(categoryType)) {
            driverText = "操作员";
        }
        priceSetAdapter = new PriceSetAdapter(LeasePriceSetActivity.this, null);
        priceSetAdapter.setUnitList(timeUnitList, isDriver,categoryType);
        binding.rvGroup.setLayoutManager(new LinearLayoutManager(LeasePriceSetActivity.this));
        binding.rvGroup.setAdapter(priceSetAdapter);
        initPriceSetList();
    }

    private void initPriceSetList() {
        if (priceSetList == null || priceSetList.size() == 0) {
            priceSetList = new ArrayList<>();
            PriceSetEntity setEntity = new PriceSetEntity();
            setEntity.setTimeNo(timeUnitList.get(0).getNo());
            setEntity.setTimeName(timeUnitList.get(0).getProjectscale());
            setEntity.setPriceUnitNo(timeUnitList.get(0).getChildren().get(0).getNo());
            setEntity.setPriceUnitName(timeUnitList.get(0).getChildren().get(0).getProjectscale());
            priceSetList.add(setEntity);
        }
        priceSetAdapter.setDatas(priceSetList);
    }

    private void validateData() {
        binding.btnSave.setFocusable(true);
        binding.btnSave.setFocusableInTouchMode(true);
        binding.btnSave.requestFocus();
        for (PriceSetEntity entity : priceSetAdapter.getDatas()) {
            if (TextUtils.isEmpty(entity.getTimeName())) {
                MToast.showToast(LeasePriceSetActivity.this, "请选择时长单位");
                return;
            } else if (isDriver && TextUtils.isEmpty(entity.getDriverPrice())) {
                if("0".equals(categoryType)){
                    MToast.showToast(LeasePriceSetActivity.this, "请输入自带驾驶员价格");
                }else{
                    MToast.showToast(LeasePriceSetActivity.this, "请输入价格");
                }
                return;
            } else if (!TextUtils.isEmpty(entity.getDriverPrice()) && Double.parseDouble(entity.getDriverPrice()) <= 0) {
                MToast.showToast(LeasePriceSetActivity.this, "请输入大于0的价格");
                return;
            } else if (!isDriver && TextUtils.isEmpty(entity.getNoDriverPrice())) {
                if("0".equals(categoryType)){
                    MToast.showToast(LeasePriceSetActivity.this, "请输入不自带驾驶员价格");
                }else{
                    MToast.showToast(LeasePriceSetActivity.this, "请输入价格");
                }
                return;
            } else if (!TextUtils.isEmpty(entity.getNoDriverPrice()) && Double.parseDouble(entity.getNoDriverPrice()) <= 0) {
                MToast.showToast(LeasePriceSetActivity.this, "请输入大于0的价格");
                return;
            }
            //是否大于最高会员价校验
            if("月".equals(entity.getTimeName()) && !TextUtils.isEmpty(entity.getDriverPrice()) && Double.parseDouble(entity.getDriverPrice()) <= maxPrice ){
                MToast.showToast(LeasePriceSetActivity.this, "当前设置价格不得小于等于会员价");
                return;
            }

//            if(!TextUtils.isEmpty(entity.getNoDriverPrice()) && Double.parseDouble(entity.getNoDriverPrice()) <= maxPrice){
//                MToast.showToast(LeasePriceSetActivity.this, "当前设置价格不得小于会员价");
//                return;
//            }
        }
        saveResult(priceSetAdapter.getDatas());
    }


    private void saveResult(List<PriceSetEntity> list) {
        Intent intent = new Intent();
        intent.putExtra("priceSetList", (Serializable) list);
        setResult(RESULT_OK, intent);
        finish();
    }

}
